package com.wyc.apiPassenger.service.impl;

import com.wyc.apiPassenger.feign.ServicePassengerUserFeignClient;
import com.wyc.apiPassenger.feign.VerificationCodeFeignClient;
import com.wyc.internalCommon.constant.IdentityContant;
import com.wyc.internalCommon.constant.TokenContant;
import com.wyc.internalCommon.request.VerificationCodeDTO;
import com.wyc.apiPassenger.service.VerificationCodeService;
import com.wyc.internalCommon.constant.CommonStatusEnum;
import com.wyc.internalCommon.util.RedisKeyUtils;
import com.wyc.internalCommon.response.NumberCodeResponse;
import com.wyc.internalCommon.response.TokenResponse;
import com.wyc.internalCommon.result.ApiResult;
import com.wyc.internalCommon.util.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;


@Service
public class VerificationCodeServiceImpl implements VerificationCodeService {

    @Autowired
    private VerificationCodeFeignClient verificationCodeFeignClient;

    @Autowired
    private ServicePassengerUserFeignClient servicePassengerUserFeignClient;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 获取验证码
     * @param passengerPhone 手机号
     * @return
     */
    @Override
    public ApiResult getVerificationCode(String passengerPhone) {
        // 调用验证码服务
        ApiResult<NumberCodeResponse> numberCodeResult = verificationCodeFeignClient.getNumberCode(6);
        NumberCodeResponse numberCodeResponse = numberCodeResult.getData();
        int numberCode = numberCodeResponse.getNumberCode();

        // 将验证码缓存到redis设置过期时间  两分钟
        String numCodeKey = RedisKeyUtils.generatorCodeKeyByPhone(passengerPhone,IdentityContant.PASSENGER_IDENTITY);
        stringRedisTemplate.opsForValue().set(numCodeKey,numberCode+"",2, TimeUnit.MINUTES);

        // 第三方服务商，发送短信

        return ApiResult.success();
    }

    /**
     * 乘客校验验证码
     * @param verificationCodeDTO
     * @return
     */
    @Override
    public ApiResult checkVerificationCode(VerificationCodeDTO verificationCodeDTO) {

        String passengerPhone = verificationCodeDTO.getPassengerPhone();
        String numCodeKey = RedisKeyUtils.generatorCodeKeyByPhone(passengerPhone,IdentityContant.PASSENGER_IDENTITY);
        // 根据手机号获取redis缓存验证码
        String redisVerificationCode = stringRedisTemplate.opsForValue().get(numCodeKey);

        // 乘客输入的验证码
        String verificationCode = verificationCodeDTO.getVerificationCode();
        // 验证码过期/验证码不正确 处理
        if (redisVerificationCode == null || !redisVerificationCode.equals(verificationCode)){
            return ApiResult.error(CommonStatusEnum.VERIFICATION_CODE_ERROR.getCode(),CommonStatusEnum.VERIFICATION_CODE_ERROR.getMsg());
        }

        // 判断是否存在该用户，无则新增用户
        servicePassengerUserFeignClient.loginOrReg(verificationCodeDTO);

        // 颁发JWT令牌 双token
        String accessToken = JWTUtils.generatorToken(passengerPhone, IdentityContant.PASSENGER_IDENTITY, TokenContant.SUCCESS_TOKEN_TYPE);
        String refreshToken = JWTUtils.generatorToken(passengerPhone, IdentityContant.PASSENGER_IDENTITY,TokenContant.REFRESH_TOKEN_TYPE);
        // redis缓存token
        String successTokenKey = RedisKeyUtils.generatorTokenKeyByPhoneWithId(passengerPhone, IdentityContant.PASSENGER_IDENTITY,TokenContant.SUCCESS_TOKEN_TYPE);
        String refreshTokenKey = RedisKeyUtils.generatorTokenKeyByPhoneWithId(passengerPhone, IdentityContant.PASSENGER_IDENTITY,TokenContant.REFRESH_TOKEN_TYPE);
        stringRedisTemplate.opsForValue().set(successTokenKey,accessToken,2,TimeUnit.HOURS);
        stringRedisTemplate.opsForValue().set(refreshTokenKey,refreshToken,3,TimeUnit.HOURS);

        TokenResponse tokenResponse = new TokenResponse();
        tokenResponse.setAccessToken(accessToken);
        tokenResponse.setRefreshToken(refreshToken);

        return ApiResult.success(tokenResponse);
    }

}
